package server;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.*;

import common.*;

public class VisualServer {
	
	private ServerThread st = null;
	private JDBC 		 db = null;

	public static void main(String[] args) {
		Runnable runnable = new Runnable()
		{
			public void run() {
				VisualServer server= new VisualServer();
				try 
				{
					server.createAndShowGUI();
				} 
				catch (UnknownHostException e) 
				{
					e.printStackTrace();
				}
			}
		};
		//invokeLater is used by java swing developer to update or perform any task on Event dispatcher thread asynchronously and thread-safety
		//Swing is not thread-safe and we CAN NOT update the Swing component or GUI from any thread.
		SwingUtilities.invokeLater(runnable);
	}

	protected void createAndShowGUI() throws UnknownHostException {

		JLabel label = null;
		
		JFrame frame = new JFrame("SERVER");
		
		frame.setSize(500, 600);
		frame.setLocation(500, 200);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new BorderLayout());
		
		final JTable table;
		//Table
		{
			table = new JTable();
			table.setFillsViewportHeight(true);
			table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			JScrollPane scrollPane = new JScrollPane(table);			
			frame.add(scrollPane, BorderLayout.CENTER);
		}
		
		final MyTextArea logs;
		//Logs
		{
			logs = new MyTextArea();
			logs.setLineWrap(true);
			logs.setRows(10);
			JScrollPane scrollPane = new JScrollPane(logs); 
			
			logs.setWrapStyleWord(true);
			logs.setEditable(false);

			logs.Append("server isn't running");
			logs.Append("test TextArea");
			logs.Append("There will be server's logs");
//			logs.Append(this.getClass().getName().replace(".", "/") + ".java");
			
			frame.add(scrollPane, BorderLayout.SOUTH);
		}
		
		// IP, port textfields
		final JTextField serverAdress;
		final JTextField serverPort;
		JPanel panel;
		{
		    panel = new JPanel();
		  
		    panel.setLayout(new GridLayout(4,2));
		    
		    label = new JLabel("Server adress: ");
		    
		    serverAdress = new JTextField(20);
		    InetAddress ipAdress=InetAddress.getLocalHost();
		    serverAdress.setText(ipAdress.getHostAddress());
		    
			panel.add(label);
			panel.add(serverAdress);
			
		    label = new JLabel("Server port: ");
		    serverPort = new JTextField(20);
		    serverPort.setText("31244");
			panel.add(label);
			panel.add(serverPort);
				
			JButton bRun;
			// Button "Run"
			bRun = new JButton("Run");
			bRun.addActionListener(new ActionListener() {	        	 
	            public void actionPerformed(ActionEvent e)
	            {
	            	try{
	                    if(st == null)
	                    {
	                    	int port = Integer.parseInt(serverPort.getText());
	                    	
	                        st = new ServerThread(
	                        		port,
	                        		InetAddress.getLocalHost(),
	                        		logs,
	                        		table);
	                        st.start();
	                    }
	                    else
	                    {
	                    	logs.Append("Server has already run");
	                    }
	                }catch(Exception exc){
	                	logs.Append("Error! " + exc.getMessage());
	                	return;
	                }
	            }
	        });
			panel.add(bRun);
			
			JButton bStop;
			//Button "Stop"
			bStop = new JButton("Stop");
			bStop.addActionListener(new ActionListener() {	   
	            public void actionPerformed(ActionEvent e)
	            {
	            	try{
	            		if(st != null){
	                        st.stopServer();
	                        st = null;
	                    }
	            		else
	            			logs.Append("Error! Server doesn't run");
	                }catch(Exception exc){
	                	logs.Append("Error! " + exc.getMessage());
	                	return;
	                }
	            }
	        });
			panel.add(bStop);
			
			frame.add(panel, BorderLayout.NORTH);
		}
	
		frame.setVisible(true);
	}
	

}
